package com.wei.czz.framework.store.manager;

import com.wei.czz.common.dto.store.audit.AuditStoreDto;
import com.wei.czz.common.dto.store.boss.BossEnumDto;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.enums.store.StoreSubmitTypeEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.vo.store.StoreBossVo;
import com.wei.czz.framework.store.entity.AuditStoreEntity;
import com.wei.czz.framework.store.entity.StoreBossEntity;
import com.wei.czz.framework.store.service.*;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-12-29 14:30:59
 * className: StoreBossManager
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class StoreBossManager {

    private static final Logger log = LoggerFactory.getLogger(StoreBossManager.class);

    private final StoreService storeService;

    private final StoreAccountService storeAccountService;

    private final StoreBossService storeBossService;

    private final AssignStoreService assignStoreService;

    private final AuditStoreService auditStoreService;

    @Transactional
    public void saveStoreBoss(StoreBossVo storeBossVo) {

        /*
            判断商店是否存在正在审核中的老板
         */
        StoreBossEntity storeBoss = storeBossService.getStoreAuditBoss(storeBossVo.getStoreId());
        if (Objects.nonNull(storeBoss)) {
            log.info("商店存在正在审核中的老板，添加失败");
            throw new CzzException("您当前有正在审核中的商店老板，添加失败。");
        }

        /*
            获取商店老板
         */
        if (Objects.nonNull(storeBossVo.getId())) {
            storeBoss = storeBossService.getStoreBoss(storeBossVo.getId());
            if (CommonEnum.ZERO.getValue().equals(storeBoss.getAuditStatus())) {
                log.info("商店老板正在审核中，不允许修改");
                throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "商店老板正在审核中，不允许修改");
            }
        }

        /*
            保存商店老板
         */
        storeBossVo.setDefaultStatus(CommonEnum.ONE.getValue());
        storeBossVo.setAuditStatus(CommonEnum.ZERO.getValue());
        storeBossService.saveStoreBoss(storeBossVo);

        /*
            添加商店老板审核分单
         */
        assignStoreService.saveAssignStore(storeBossVo.getStoreId(),
                StoreSubmitTypeEnum.ADD_STORE_BOSS.getValue());
    }

    public BossEnumDto getStoreBossEnums(Long id, Long storeId) {

        /*
            获取下一个排序等级
         */
        Integer sort = storeBossService.getNextSort(storeId);

        /*
            商店老板最新一条审核拒绝记录
         */
        AuditStoreDto auditStoreDto = null;
        if (Objects.nonNull(id)) {
            List<AuditStoreEntity> auditStoreList = auditStoreService.getAuditStoreList(null, null,
                    id);
            auditStoreDto = auditStoreList.stream()
                    .filter(auditStore -> CommonEnum.ONE.getValue().equals(auditStore.getStatus()))
                    .max(Comparator.comparing(AuditStoreEntity::getId))
                    .map(AuditStoreDto::map)
                    .orElse(null);
        }

        BossEnumDto bossEnumDto = new BossEnumDto();
        bossEnumDto.setSort(sort)
                .setAuditStoreDto(auditStoreDto);
        return bossEnumDto;
    }

    @Transactional
    public void updateStoreBoss(StoreBossVo storeBossVo) {

        /*
            获取商店老板
         */
        StoreBossEntity storeBoss = storeBossService.getStoreBoss(storeBossVo.getId());
        if (CommonEnum.ZERO.getValue().equals(storeBoss.getDefaultStatus())) {
            log.info("商店默认老板不允许直接修改");
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "商店默认老板不允许直接修改");
        }

        /*
            校验商店是否存在正在审核中的老板
         */
        storeBoss = storeBossService.getStoreAuditBoss(storeBossVo.getStoreId());
        if (Objects.nonNull(storeBoss)) {
            throw new CzzException("您当前有正在审核中的商店老板，添加失败。");
        }

        /*
            保存商店老板
         */
        storeBossVo.setDefaultStatus(CommonEnum.ONE.getValue());
        storeBossVo.setAuditStatus(CommonEnum.ZERO.getValue());
        storeBossService.saveStoreBoss(storeBossVo);

        /*
            添加商店老板审核分单
         */
        assignStoreService.saveAssignStore(storeBossVo.getStoreId(),
                StoreSubmitTypeEnum.STORE_BOSS_MODIFY.getValue());
    }

    @Transactional
    public void deleteStoreBoss(Long id) {
        /*
            获取商店老板
         */
        StoreBossEntity storeBoss = storeBossService.getStoreBoss(id);
        if (CommonEnum.ZERO.getValue().equals(storeBoss.getDefaultStatus())) {
            log.info("商店默认老板不允许删除");
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "商店默认老板不允许删除");
        }

        /*
            删除商店老板
         */
        storeBossService.deleteStoreBoss(id);

        /*
            删除商店老板审核分单
         */
        if (CommonEnum.ZERO.getValue().equals(storeBoss.getAuditStatus())) {
            List<Integer> submitTpeList = Arrays.asList(StoreSubmitTypeEnum.ADD_STORE_BOSS.getValue(),
                    StoreSubmitTypeEnum.STORE_BOSS_MODIFY.getValue());
            assignStoreService.batchDeleteAssignStore(Collections.singletonList(storeBoss.getStoreId()), submitTpeList);
        }
    }
}
